package com.extr.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.extr.controller.domain.QuestionQueryResult;
import com.extr.domain.exam.HomeWork;
import com.extr.domain.exam.HomeWorkHistory;
import com.extr.domain.exam.Paper;
import com.extr.domain.question.QuestionStruts;
import com.extr.persistence.HomeWorkMapper;
import com.extr.persistence.HomeWorkingMapper;
import com.extr.persistence.QuestionMapper;
import com.extr.util.Page;
import com.extr.util.xml.Object2Xml;

@Service("homeWorkService")
public class HomeWorkServiceImpl implements HomeWorkService {

	private static Logger log = Logger.getLogger(HomeWorkServiceImpl.class);
	@Autowired
	private QuestionMapper questionMapper;
	@Autowired
	private HomeWorkMapper homeWorkMapper;
	@Autowired
	private HomeWorkingMapper homeWorkingMapper;
	
	@Override
	public void createHomeWork(
			HashMap<Integer, HashMap<Integer, List<QuestionStruts>>> questionMap,
			HashMap<Integer, Integer> questionTypeNum,
			HashMap<Integer, Float> questionTypePoint,
			HashMap<Integer, Float> knowledgePointRate, HomeWork homeWork) {
		Paper paper = new Paper(questionMap, questionTypeNum, questionTypePoint,knowledgePointRate);
		try {
			paper.createPaper();
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e1.getMessage());
		}

		try {
			HashMap<Integer, QuestionStruts> paperQuestionMap = paper
					.getPaperQuestionMap();

			Iterator<Integer> it = paperQuestionMap.keySet().iterator();
			List<Integer> idList = new ArrayList<Integer>();
			while (it.hasNext()) {
				idList.add(it.next());
			}
			List<QuestionQueryResult> questionList = questionMapper
					.getQuestionAnalysisListByIdList(idList);
			for(QuestionQueryResult qqr : questionList){
				qqr.setQuestionPoint(questionTypePoint.get(qqr.getQuestionTypeId()));
			}
			homeWork.setContent(Object2Xml.toXml(questionList));
			homeWorkMapper.insertHomeWork(homeWork);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public void insertHomeWork(HomeWork homeWork) {
		this.homeWorkMapper.insertHomeWork(homeWork);
	}

	@Override
	public List<HomeWork> getHomeWorkList(Page<HomeWork> pageModel) {
		return this.homeWorkMapper.getHomeWorkList(pageModel);
	}

	@Override
	public HomeWork getHomeWorkById(int homeworkid) {
		return this.homeWorkMapper.getHomeWorkById(homeworkid);
	}

	@Override
	public List<QuestionQueryResult> getQuestionDescribeListByIdList(
			List<Integer> idList) {
		List<QuestionQueryResult> questionList = questionMapper.getQuestionAnalysisListByIdList(idList);
		return questionList;
	}

	@Override
	public void updateHomeWork(HomeWork homework) {
		this.homeWorkMapper.updateHomeWork(homework);
	}

	@Override
	public void deleteHomeWork(Integer homeWorkId) {
		this.homeWorkMapper.deleteHomeWork(homeWorkId);
	}

	@Override
	public List<HomeWork> getIndexHomeWorkList() {
		return this.homeWorkMapper.getIndexHomeWorkList();
	}

	@Override
	public HomeWorkHistory getUserHomeWorkHistoryByUserIdAndHomeWorkId(int userId, int homeWorkId) {
		return this.homeWorkingMapper.getUserHomeWorkHistoryByUserIdAndHomeWorkId(userId, homeWorkId);
	}

	@Override
	public void addUserHomeWorkHistory(HomeWorkHistory homeWorkHistory) {
		this.homeWorkingMapper.addUserHomeWorkHistory(homeWorkHistory);
	}

	@Override
	public HomeWorkHistory getUserHomeWorkHistoryByHistId(
			int homework_history_id) {
		return homeWorkingMapper.getUserHomeWorkHistoryByHistId(homework_history_id);
	}

	@Override
	public void updateHomeWorkHistory(HomeWorkHistory homeWorkHistory) {
		homeWorkingMapper.updateHomeWorkHistory(homeWorkHistory);
	}

	@Override
	public List<HomeWorkHistory> getHomeWorkHistByHomeWorkId(int homeWorkId, Page<HomeWorkHistory> pageModel) {
		return this.homeWorkingMapper.getHomeWorkHistByHomeWorkId(homeWorkId, pageModel);
	}

	@Override
	public void updateHomeWorkHistoryOfTeacher(HomeWorkHistory homeWorkHistory) {
		this.homeWorkingMapper.updateHomeWorkHistoryOfTeacher(homeWorkHistory);
	}

	@Override
	public List<HomeWorkHistory> getHomeWorkHistByUserId(int userid,
			Page<HomeWorkHistory> pageModel) {
		return this.homeWorkingMapper.getHomeWorkHistByUserId(userid, pageModel);
	}
}
